﻿using System.ComponentModel;
using System.Reflection;
using System.Text.RegularExpressions;

namespace HYHY_Core.Tool.Tool
{
    /// <summary>
    /// 获取枚举公共类
    /// </summary>
    static public class EnumCommon
    {
        /// <summary>
        /// 获取枚举定义值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            var field = value.GetType().GetField(value.ToString());
            var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
            return customAttribute == null ? value.ToString() : ((DescriptionAttribute)customAttribute).Description;
        }
    }

    /// <summary>
    /// 工具类
    /// </summary>
    public static class ToolHelper
    {
        /// <summary>
        /// 去掉入参前后空格
        /// </summary>
        public static void TrimStrings(object obj)
        {
            PropertyInfo[] properties = obj.GetType().GetProperties();
            foreach (var property in properties)
            {
                if (property.PropertyType == typeof(string))
                {
                    string value = (string)property.GetValue(obj);
                    if (value != null)
                    {
                        value = value.Trim();
                        property.SetValue(obj, value);
                    }
                }
            }
        }

        /// <summary>
        /// 手机号正则校验
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public static bool IsValidPhoneNumber(string phoneNumber)
        {
            // 该方法使用正则表达式^1[3 - 9]\d{9}$来验证手机号格式。这个正则表达式的含义是：以 1 开头，第二位是 3 - 9 中的一个数字，后面跟着 9 位数字
            string pattern = @"^1[3-9]\d{9}$";
            return Regex.IsMatch(phoneNumber, pattern);
        }
    }
}
